package logic

import (
	"context"

	"mail/service/outlay/model"
	"mail/service/outlay/rpc/internal/svc"
	"mail/service/outlay/rpc/outlay"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type WritLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWritLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WritLogic {
	return &WritLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *WritLogic) Writ(in *outlay.WritRequest) (*outlay.WritResponse, error) {
	// todo: add your logic here and delete this line
	//沉思的、体贴的
	thoughtful, err := l.svcCtx.OutlayModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100, "outlay isn't exixts.")
		}
		return nil, status.Error(500, err.Error())
	}
	return &outlay.WritResponse{
		Id:     thoughtful.Id,
		Uid:    thoughtful.Uid,
		Rid:    thoughtful.Rid,
		Amount: thoughtful.Amount,
		Source: thoughtful.Source,
		Status: thoughtful.Status,
	}, nil
}
